import { reactive, ref } from "vue";
import { getListApi } from "@/api/configCenter/templateLibrary/index";

export default function InitHook() {
  //表单数据
  const formState = reactive<Partial<TemplateLibrary.AddForm>>({
    name: undefined,
    description: undefined,
    typeCode: undefined,
    type: undefined,
    fieldList: [],
    nameList: [],
    exportName: undefined
  });

  const dataList = ref<Array<TemplateLibrary.dataList>>([]);

  const saveOrExportStatus = ref<string>("1"); //导出或保存的状态值  1直接导出 2保存为模版并导出

  //表格数据
  const tableData = reactive([]);

  //等待状态
  const loading = ref<boolean>(false);

  //重置数据
  const resetDataList = () => {
    dataList.value.map((t: any) => {
      t.valueData = [];
      t.checkValue = [];
    });
  };

  //获取导出内容
  const getExportContentApi = () => {
    loading.value = true;
    getListApi()
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          dataList.value = data.map((t: any) => {
            return { ...t, checkValue: [] };
          });
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  //表单ref
  const formRef = ref(null);
  return {
    tableData,
    formState,
    loading,
    formRef,
    getExportContentApi,
    dataList,
    resetDataList,
    saveOrExportStatus
  };
}
